package com.atguigu.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/Servlet06GetSession")
public class Servlet06GetSession extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();

        // 1、通过 request 对象获取 HttpSession 对象
        HttpSession session = request.getSession();

        // 2、查看当前 session 对象是否是新的
        boolean isNew = session.isNew();
        writer.write(isNew?"当前 session 对象新创建的":"当前 session 对象是旧的");
        writer.write("<br/>");

        // 3、获取 session 的 id
        String id = session.getId();
        writer.write("当前 session 对象的 id = " + id + "<br/>");

        // 4、获取 session 对象目前的最大空闲时间
        int maxInactiveInterval = session.getMaxInactiveInterval();
        writer.write("当前 session 对象的最大空闲时间 = " + maxInactiveInterval + "秒<br/>");
    }
}
